package com.feicheng.iou.result;


import com.alibaba.fastjson.JSONObject;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.util.CusStringUtil;
import com.feicheng.iou.util.ReadPropertiesUtil;

import java.util.Properties;

public class ResultInfo extends JSONObject {

    //获取程序当前路径下的文件
    public static final Properties properties = ReadPropertiesUtil
            .getProperties("/responseMessage.properties");

    private static final long serialVersionUID = -1412532553323522836L;

    /**
     * @return
     * @Author tianchunlei
     * @Description 默认构造，返回操作正确的返回代码和信息
     * @Date 2021/7/7 15:12
     * @Param
     */
    private ResultInfo() {
        this.put("CODE", CusStringUtil.removeMarks(properties.get(ReturnCode.SUCCESS.val()).toString()));
        this.put("MESSAGE", CusStringUtil.removeMarks(properties.get(ReturnCode.SUCCESS.msg()).toString()));
        this.put("STATUS", "SUCCESS");
    }

    /**
     * @return
     * @Author tianchunlei
     * @Description //默认值返回，默认返回正确的code和message
     * @Date 2021/7/7 15:12
     * @Param
     */
    private ResultInfo(Object data) {
        this.put("CODE", CusStringUtil.removeMarks(properties.get(ReturnCode.SUCCESS.val()).toString()));
        this.put("MESSAGE", CusStringUtil.removeMarks(properties.get(ReturnCode.SUCCESS.msg()).toString()));
        this.put("DATA", data);
        this.put("STATUS", "SUCCESS");
    }

    /**
     * @return
     * @Author tianchunlei
     * @Description //构造返回代码，以及自定义的错误信息
     * @Date 2021/7/7 15:13
     * @Param
     */
    private ResultInfo(ReturnCode returnCode) { //响应fail
        this.put("CODE", CusStringUtil.removeMarks(properties.get(returnCode.val()).toString()));
        this.put("MESSAGE", CusStringUtil.removeMarks(properties.get(returnCode.msg()).toString()));
        this.put("STATUS", "FAIL");
    }

    private ResultInfo(ReturnCode returnCode, String msg) { //响应ERROR和LOGIN_FAIL
        this.put("CODE", CusStringUtil.removeMarks(properties.get(returnCode.val()).toString()));
        this.put("MESSAGE", CusStringUtil.removeMarks(properties.get(returnCode.msg()).toString()));
        this.put("STATUS", msg);
    }

    /**
     * @return
     * @Author tianchunlei
     * @Description //响应登陆失败
     * @Date 2021/7/20 09:43
     * @Param
     */


    public static ResultInfo success() {
        return new ResultInfo();
    }

    public static ResultInfo success(Object data) {
        return new ResultInfo(data);
    }

    public static ResultInfo fail(ReturnCode returnCode) {
        return new ResultInfo(returnCode);
    }

    public static ResultInfo error() {
        return new ResultInfo(ReturnCode.ERROR, "ERROR");
    }

    public static ResultInfo loginFail() {
        return new ResultInfo(ReturnCode.LOGIN_FAIL, "LOGIN_FAIL");
    }
}
